﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Timers;
using HistoryToMail;

namespace IM
{
    public class MessageToSend
    {
        public String Subject;
        public String Body;
        public String Sender;
    }

    public delegate void OnMessageTosendAvailable(MessageToSend msg);


    public class IMMessagesStore
    {
        private Dictionary<String, IMProtocol> storage = new Dictionary<String, IMProtocol>();
        private Timer timer;
        private PluginSettings pluginSettings;
        private Boolean pushWhenStop = false;

        public event OnMessageTosendAvailable OnMsgToSend;

        public IMMessagesStore(ref PluginSettings pluginSettings)
        {
            this.pluginSettings = pluginSettings;
            timer = new System.Timers.Timer(30000);
            timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            timer.Start();
        }

        public void PushhMessageSendProcessAndStop()
        {
            timer.Stop();
            pushWhenStop = true;
            OnTimedEvent(null, null);
        }

        public void changeSettings(ref PluginSettings pluginSettings)
        {
            this.pluginSettings = pluginSettings;
        }

        public void addMessage(String protocol, String sender, String receiver,
                       HistoryToMail.MessageType Type, String body, DateTime time)
        {
            IMProtocol prot = null;
            if (storage.ContainsKey(protocol))
            {
                prot = storage[protocol];
            }
            else
            {
                prot = new IMProtocol();
                storage[protocol] = prot;
            }
            prot.addMessage(sender, receiver, Type, body, time);
        }

        private void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            if (OnMsgToSend == null) return;
            foreach (String protName in storage.Keys)
            {
                IMProtocol prot = storage[protName];
                foreach (String cntctName in prot.contacts.Keys)
                {
                    IMContact cntct = prot.contacts[cntctName];
                    if (!cntct.readyToSend(pluginSettings) && !pushWhenStop) continue;


                    MessageToSend msg = new MessageToSend();
                    msg.Sender = cntctName;
                    msg.Subject = protName + " " + cntctName + " " + cntct.lastMessageTime;
                    msg.Body =
                    "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">" +
                    "<html>" +
                    "<head>" +
                    "" +
                    "<meta http-equiv=\"content-type\" content=\"text/html; charset=ISO-8859-1\">" +
                    "</head>" +
                    "<body bgcolor=\"#ffffff\" text=\"#000000\">" +
                    pluginSettings.computerName +"<BR>"+ 
                    "<table border=\"0\" cellpadding=\"2\" cellspacing=\"2\">" +
                    "  <tbody>";


                    
                    
                    List<IMMessage> msgList = cntct.getMessagesForHistory();
                    if (msgList.Count == 0) continue;
                    foreach (IMMessage message in msgList)
                    {
                        msg.Body +=
                        "    <tr>";
                        msg.Body += "    <td valign=\"top\">" + message.time.ToShortTimeString() + "<br>";
                        msg.Body += 
                        "      </td>" +
                        "      <td valign=\"top\" ><b>"  + message.interlocutor + ":</b> " + message.Body.Replace("\r\n", "<br>") +
                        "      </td>";
                        msg.Body += "    </tr>";
                    }
                    msg.Body +=
                        "  </tbody>" +
                        "</table>" +
                        "<br>" +
                        "</body>" +
                        "<BR><a style=\"color: rgb(255, 255, 255);\">" + pluginSettings.mailFilterLabel +
                        "</a></html>";


                    if (OnMsgToSend != null)
                    {
                        OnMsgToSend(msg);
                    }
                }
            }
        }
    }
}
